package com.xjrsoft.module.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.EnabledMark;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageInput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.system.dto.AddTreeDto;
import com.xjrsoft.module.system.dto.UpdateTreeDto;
import com.xjrsoft.module.system.entity.Tree;
import com.xjrsoft.module.system.service.ITreeService;
import com.xjrsoft.module.system.vo.TreeListVo;
import com.xjrsoft.module.system.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zlf
 * @since 2024-02-23
 */
@RestController
@RequestMapping(GlobalConstant.SYSTEM_MODULE_PREFIX + "/tree")
@Api(value = GlobalConstant.SYSTEM_MODULE_PREFIX + "/tree", tags = "树接口管理")
@AllArgsConstructor
public class TreeController {

    private final ITreeService treeService;

    @GetMapping("/list")
    @ApiOperation("查询树结构列表数据(不分页)")
    public R list() {
        List<Tree> treeList = treeService.list(Wrappers.lambdaQuery(Tree.class).eq(Tree::getEnabledMark, EnabledMark.ENABLED.getCode()));
        return R.ok(BeanUtil.copyToList(treeList, TreeListVo.class));
    }

    @GetMapping("/page")
    @ApiOperation("查询树结构列表数据(分页)")
    public R page(@Valid PageInput dto) {
        IPage<TreeListVo> page = treeService.selectJoinListPage(ConventPage.getPage(dto), TreeListVo.class,
                MPJWrappers.<Tree>lambdaJoin()
                        .disableSubLogicDel()
                        .select(Tree::getId)
                        .select(Tree.class, x -> VoToColumnUtil.fieldsToColumns(TreeListVo.class).contains(x.getProperty()))
                        .leftJoin(User.class, User::getId, Tree::getCreateUserId, ext -> ext.selectAs(User::getName, TreeListVo::getCreateUserName))
                        .leftJoin(User.class, User::getId, Tree::getModifyUserId, ext -> ext.selectAs(User::getName, TreeListVo::getModifyUserName))
                        .and(StrUtil.isNotEmpty(dto.getKeyword()),
                        wrapper -> wrapper.like(Tree::getName, dto.getKeyword()).or().like(Tree::getCode, dto.getKeyword())));
        return R.ok(ConventPage.getPageOutput(page));
    }

    @GetMapping("/info")
    @ApiOperation("查询树结构详情数据")
    public R info(@RequestParam Long id) {
        Tree tree = treeService.getById(id);
        return R.ok(BeanUtil.toBean(tree, TreeVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增树结构")
    public R add(@Valid @RequestBody AddTreeDto dto) {
        Tree tree = BeanUtil.toBean(dto, Tree.class);
        return R.ok(treeService.save(tree));
    }

    @PutMapping
    @ApiOperation(value = "更新树结构")
    public R update(@Valid @RequestBody UpdateTreeDto dto){
        Tree tree = BeanUtil.toBean(dto, Tree.class);
        return R.ok(treeService.updateById(tree));
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public R delete(@RequestBody List<Long> ids) {
        if (ids.contains(1L)) {
            return R.error("不能删除主系统！");
        }
        return R.ok(treeService.removeBatchByIds(ids));
    }

    @GetMapping("/tree-list")
    @ApiOperation("查询树结构详情数据")
    public R treeList(@RequestParam(defaultValue = "") String keyword) {
        return R.ok(treeService.treeList(keyword));
    }
}
